/*===========================================================
 
 .---=||||[ B L O O D Y ]|||||=====================----------....
 |                                                              |
 |    THE  M O S T   B L O O D Y  GAME EVER!                    |
 |                                                              |
 |    bloody harvest is coming... soon,                         |
 |                                              heil Satan!     |
 !                                                              !
 .                                                              ,
  ~~~~-------------================================|||||||||==-~




 Name-Space: {core, logic, editor}
 Module: {draw, coll, scgr, ...}
 Class: <name of class>
 Last modification:  <author>, <time>

 TODO:
 <-------- todo #1 ---------->
 <-------- todo #2 ---------->
 <-------- todo #3 ---------->
        ............
 <-------- todo #n ---------->

 FIX:
 <-------- fix #1 ----------->
 <-------- fix #1 ----------->
         ..........
 <-------- fix #n ----------->




 
 
 ============================================================*/
#include "core/draw/Light.h"

namespace core
{
	namespace draw
	{

		Light::Light(u32 aLightNum)
		{
			ambient = Vector4f(0, 0, 0, 1);
			diffuse = Vector4f(0, 0, 0, 0);
			specular = Vector4f(0, 0, 0, 0);
			position = Vector3f(0, 0, 1);
			attenuation = Vector3f(1, 0, 0);

			lightNo = aLightNum;
			enabled = false;
		}

		Light::~Light()
		{
		}

		void Light::bind()
		{
			GLenum light = GL_LIGHT0 + lightNo;
			
			Vector4f pos = Vector4f(position.x, position.y, position.z, 0);
			glLightfv(light, GL_AMBIENT, ambient);
			glLightfv(light, GL_DIFFUSE, diffuse);
			glLightfv(light, GL_SPECULAR, specular);
			glLightfv(light, GL_POSITION, pos);
			glLightf(light, GL_CONSTANT_ATTENUATION, attenuation.x);
			glLightf(light, GL_LINEAR_ATTENUATION, attenuation.y);
			glLightf(light, GL_QUADRATIC_ATTENUATION, attenuation.z);

			glEnable(light);
		}

		void Light::unbind()
		{
			glDisable(GL_LIGHT0 + lightNo);
		}

		Light* Light::fromConfig(const Config& cfg)
		{
			Light* ret = new Light();
			if (cfg.hasKey("ambient"))
			{
				cfg["ambient"].to(ret->ambient);
			}
			if (cfg.hasKey("diffuse"))
			{
				cfg["diffuse"].to(ret->diffuse);
			}
			if (cfg.hasKey("specular"))
			{
				cfg["specular"].to(ret->specular);
			}
			if (cfg.hasKey("attenuation"))
			{
				cfg["attenuation"].to(ret->attenuation);
			}
			return ret;
		}
	}
}
